package model.trader.trade;
//package model.trader.strategy;
//
//
//import model.market.BankOfCredit;
//import model.market.MarketException;
//import model.market.DayMarketInfo;
//import model.market.MarketManager;
//import model.market.Position;
//import model.market.Trade;
//import model.trader.Trader;
//
//public class ActivePortfolioTrade {
//	//AMXX: the market impact stuff is really weak.. we are only encouraging momentum to short and discouraging going long.
//	
//	//AM: this is to avoid the problem of a trader selling as soon as they have bought the asset
//	//		and shifted the price.. it gives them a 10% band to work with for their portfolio allocation.
//	public static double THRESHOLD_PORTFOLIO_REBALANCE = .05;
//
//	private BankOfCredit bank = new BankOfCredit();
//
//	
//	private static final double GOOD_DEAL_PRICE_BUFFER = 1.00;//AMXX: was 1.02
//	
//	private double MARGIN_BUFFER=0.5;//Keep some money to avoid margin call.
//	
//	public void determineTradeLogical(Trader trader, MarketManager market, 
//			Position position, 
//			double fairPrice) {
//		//AMXX: working here.
//		System.out.println(""+trader.getClass()+" fair: "+fairPrice+" market "+market.getMarketPrice());
//		if(fairPrice<0){
//			throw new MarketException("Fairprice less than zero "+fairPrice+" trader "+trader.getClass());
//		}
//		double marketPrice = market.getMarketPrice();
//		int currentPosition = position.getExecutedQuantity();
//		if(marketPrice>fairPrice){
//			//AM: lets short it.
//			//AM: this extra return calc is wrong if we have lending, divvy, and risk free rates different.
//			double expectedExtraReturn = (marketPrice-fairPrice)/fairPrice;
//			double workingCapital = bank.getPossibleShortPosition(market, position, MARGIN_BUFFER);
//
//			if(currentPosition>0){
//				//We should close our long...
//				//System.out.println("Closing at limit: "+fairPrice/GOOD_DEAL_PRICE_BUFFER);
//				Trade close = new Trade(trader,currentPosition,Trade.SELL,fairPrice/GOOD_DEAL_PRICE_BUFFER);//.95 forces shift.
//				market.order(close);
//				//AMXX: play with this one day..decreasePosition(trader, market, position, currentPosition, 0, Trade.SELL);
//			}else{
//				//AM: short the fu*ker!!
//				double desiredPositionValue = (Math.min(expectedExtraReturn,1))*workingCapital;
//				
//				//AM: note no market impact calc .
//				int desiredQuantity =(int) (desiredPositionValue/marketPrice);
//				int absPosition = Math.abs(currentPosition);
//				
//				if(currentPosition<0&&Math.abs(((double)desiredQuantity)/absPosition-1)<THRESHOLD_PORTFOLIO_REBALANCE){
//					//AM: dont bother.
//				}else if(desiredQuantity>absPosition){
//					Trade purchase = new Trade(trader,desiredQuantity-absPosition,Trade.SELL,fairPrice);
//					market.order(purchase);
//				}else if(desiredQuantity<absPosition){
//					decreasePosition(trader, 
//							market, 
//							position, 
//							absPosition, 
//							desiredQuantity, 
//							Trade.BUY);
//					
//				}
//				
//			}
//			
//		}else{
//			//AM: lets buy it.
//			double expectedExtraReturn = (fairPrice - marketPrice)/fairPrice;
//			double workingCapital = bank.getPossibleLongPosition(market, position, MARGIN_BUFFER);
//
//			if(currentPosition<0){
//				//AMXX: allow a burst straight trough zero to the other side.
//				//We should close our short...
//				Trade close = new Trade(trader,-currentPosition,Trade.BUY,fairPrice*GOOD_DEAL_PRICE_BUFFER);
//				market.order(close);
//				
//				//AMXX: play with this one day.. decreasePosition(trader, market, position, -currentPosition, 0, Trade.BUY);
//			}else{
//				double desiredPositionValue = (Math.min(expectedExtraReturn,1))*(workingCapital);
//				
//				if(desiredPositionValue <0){
//					throw new MarketException("desire less than zero?"+
//												trader.getClass()+" fairprice "+fairPrice);
//				}
//				//AM: mnote no market impact calc...
//				int desiredQuantity =(int) (desiredPositionValue/marketPrice);
//				
//				if(currentPosition>0&&
//						Math.abs(((double)desiredQuantity)/currentPosition-1)<THRESHOLD_PORTFOLIO_REBALANCE){
//					//AM: dont bother.
//				}else if(desiredQuantity>currentPosition){
//					Trade purchase = new Trade(trader,desiredQuantity-currentPosition,Trade.BUY,fairPrice);
//					market.order(purchase);
//				}else if(desiredQuantity<currentPosition){
//					decreasePosition(trader, 
//							market, 
//							position, 
//							currentPosition, 
//							desiredQuantity, 
//							Trade.SELL);
//				}
//			}
//			
//		}
//	}
//	
//	/**
//	 * 
//	 * @param trader 
//	 * @param market
//	 * @param position - the trader's position
//	 * @param fairPrice - the fair price as determined by the trader.
//	 * 
//	 */
//	public void determineTrade(Trader trader, MarketManager market, 
//							Position position, 
//							double[] fairPrices) {
//		
////AMTEMPXX:		boolean includeProspectTheory = market.getConfig().includeProspectTheory();
////		
////		if(includeProspectTheory){
////			Boolean tradeRecommendation= new BehaviouralTraderPatterns()
////					.prospectTheoryTradeRecomendation(trader, position, market, .1, 0.01);
////			if(tradeRecommendation==null){
////				//well our emotions didnt find any conclusions, so we act more rationally.
////				determineTradeLogical(trader, market, position, fairPrice);
////				
////			}else if(tradeRecommendation.booleanValue()){
////				//An appropriate profit has been made, cash out..
////				//AMXX: this is too blunt..need to incorporate price info..
////				market.closeOrder(position);
////			}
////		}else{
//			determineTradeLogical(trader, market, position, fairPrices);
////		}
//		
//		
//	}
//
//
//	private void decreasePosition(Trader trader, 
//						MarketManager market, 
//						Position position, 
//						int absPosition, 
//						int desiredQuantity, boolean closeDirection) {
//		//DayMarketInfo[] prices = market.getPrices();
//		boolean depressed=true;
//		
//		
//		
//		//AM: trade differently if taking profit vs. avoiding a margin call..
//		if(prices.length>1){
//			double worth =position.getWorth(market.getMarketPrice());
//			double yesterWorth = position.getYesterdayWorth(market.getPeriod());
//			if(yesterWorth<=worth){
//				//we are in a rising market, i.e we are about to take profit.
//				depressed=false;
//			}
//			
//		}
//		
//		
//		//System.out.println("depressed "+depressed+" "+market.getPeriod()+" current pos: "+absPosition+" new quant "+desiredQuantity);
//		
//		if(depressed){
//			//AM: risk control..
//			Trade purchase = new Trade(trader,desiredQuantity-absPosition,closeDirection);
//			market.order(purchase);
//		}else{
//			//AM: profit taking..pop a cap on it..
//			Trade purchase = new Trade(trader,desiredQuantity-absPosition,closeDirection,market.getMarketPrice());
//			market.order(purchase);
//		}
//	}
//	
//	
//}
